package com.nextwist.impl;

import java.util.List;

import javax.ws.rs.core.Response;

import com.nexttwist.common.BaseException;
import com.nexttwist.common.BasicValidator;
import com.nexttwist.common.UserValidator;
import com.nexttwist.common.Util;
import com.nexttwist.pojo.User;
import com.nexttwist.pojo.UserStory;
import com.nextwist.dao.CommonDAO;
import com.nextwist.dao.UserDAO;
import com.nextwist.interfaces.IUserInterface;

/**
 * This method is used to implement the user related functions
 * @author Mintu
 * @version 0.1
 */
public class UserImpl implements IUserInterface {

	UserDAO userDAO = new UserDAO();
	CommonDAO commonDAO = new CommonDAO();
	BasicValidator basicValidator = new BasicValidator();
	UserValidator userValidator = new UserValidator();
	private boolean isAddedSuccess;
	
	/**
	 * This method is used to find the Stories created by the particular user
	 * @param userId
	 * @param pageNo
	 * @param limit
	 * @return JsonArray
	 */
	@Override
	public Response userSpecificStory( int userId, int pageNo, int limit){
		try {
			int start = (pageNo-1)*limit + 1;
			if(!commonDAO.isUserExists(userId))
				throw new Exception("User with userID- "+ userId +" doesn't exist");
			List<UserStory> userStoryList = userDAO.userSpecificStory(userId, start, limit);
			//System.out.println(testArray.getJSONObject(0).getString("storyId"));
			return Util.successResponse(userStoryList);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		} catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response addUser(User userObject) {
		try{
			userValidator.validateUser(userObject);
			 isAddedSuccess = userDAO.userAdd(userObject);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
		// TODO Auto-generated method stub
		 return Util.successResponse(isAddedSuccess);
	}
	
	@Override
	public Response updateUser(User userObject){
		try{
			userValidator.validateUser(userObject);
			if(!commonDAO.isUserExists(userObject.getUserId()))
				throw new Exception("User with userID- "+ userObject.getUserId() +" doesn't exist");
			if(userObject.getIsEmailChange()){
				if(commonDAO.isEmailAvailable(userObject.getEmail())){return Util.failureResponse("Email is already exist.");
				}
			}
			User userObj = userDAO.userUpdate(userObject);
			return Util.successResponse(userObj);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response userFollowers(int userId, int pageNo, int limit) {
		try{
			if(!commonDAO.isUserExists(userId))
				throw new Exception("User with userID- "+ userId +" doesn't exist");
			int start = (pageNo-1)*limit;
			List<User> userList = userDAO.userFollower(userId, start, limit);
			return Util.successResponse(userList);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response userFollowings(int userId, int pageNo, int limit) {
		try{
			if(!commonDAO.isUserExists(userId))
				throw new Exception("User with userID- "+ userId +" doesn't exist");
			int start = (pageNo-1)*limit;
			List<User> userList = userDAO.userFollowing(userId, start, limit);
			return Util.successResponse(userList);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response userFollow(int followerId, int followingId){
		try{
			if(!commonDAO.isUserExists(followerId))
				throw new Exception("User with userID- "+ followerId +" doesn't exist");
			if(!commonDAO.isUserExists(followingId))
				throw new Exception("User with userID- "+ followingId +" doesn't exist");
			String ret = userDAO.followUser(followerId, followingId);
			return Util.successResponse(ret);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response userDetails(int userId) {
		try{
			User userDetail = userDAO.getUser(userId);
			return Util.successResponse(userDetail);
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response changePassword(int userId, String oldPass, String newPass){
		try{
			if(!commonDAO.isUserExists(userId))
				throw new Exception("User with userID- "+ userId +" doesn't exist");
			return Util.successResponse(userDAO.changePassword(userId, oldPass, newPass));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

	@Override
	public Response userLogin(String emailId, String password){
		try{
			return Util.successResponse(userDAO.loginUser(emailId, password));
		} catch (BaseException e) {
			return Util.failureResponse(e.getMessage());
		}catch (Exception e) {
			return Util.failureResponse(e.getMessage());
		}
	}

}
